package org.xq.mq.controller;

import org.xq.mq.component.GoodsConsumer;
import org.xq.mq.component.OrderConsumer;
import org.xq.mq.component.RedisChannelPublisher;
import org.xq.mq.component.RedisQueueProducer;
import org.xq.mq.model.OrderInfoReq;
import org.xq.mq.model.RedisQueueMessage;
import org.xq.mq.util.HttpUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.Random;

@RestController
public class OrderController {

    @Autowired
    OrderConsumer orderConsumer;
    @Autowired
    GoodsConsumer goodsConsumer;
    @Autowired
    RedisQueueProducer redisQueueProducer;
    @Autowired
    RedisChannelPublisher redisChannelPublisher;


    /**
     * mq接收web的流量洪峰,将消息推到队列中...
     * 同时会有消费者进行消费
     * @param orderInfoReq
     */
   @PostMapping("/receive_msg")
    public void send_msg(@RequestBody OrderInfoReq orderInfoReq) {
        String queueName = orderConsumer.getQueueName();
        RedisQueueMessage redisQueueMessage =
                new RedisQueueMessage(JSON.toJSONString(orderInfoReq));
        redisQueueProducer.sendMessage(queueName, redisQueueMessage);
    }




    @GetMapping("/test")
    public void test() {
        String queueName = orderConsumer.getQueueName();
        RedisQueueMessage redisQueueMessage =
                new RedisQueueMessage("[order生产者] 发送的测试消息=" + System.currentTimeMillis() + getRandomChar());
        redisQueueProducer.sendMessage(queueName, redisQueueMessage);
    }


    @GetMapping("/test2")
    public void test2() {
        String queueName = orderConsumer.getQueueName();

        for (int i = 0; i < 1000; i++) {
            RedisQueueMessage redisQueueMessage =
                    new RedisQueueMessage("[order生产者] 发送的测试消息=" + System.currentTimeMillis() + getRandomChar());
            redisQueueProducer.sendMessage(queueName, redisQueueMessage);
        }
    }


    @GetMapping("/test3")
    public void test3() {
        String queueName = orderConsumer.getQueueName();

        for (int i = 1; i <= 30000; i++) {
            RedisQueueMessage redisQueueMessage =
                    new RedisQueueMessage(
                            "{\"user_id\":1,\"product_id\":1,\"product_count\":1,\"order_desc\":\" "+i+getRandomChar()+"\" }"
                    );
            redisQueueProducer.sendMessage(queueName, redisQueueMessage);
        }
    }







    @GetMapping("/test4")
    public void test4() {
        try {
            HttpUtil.post("http://localhost:8080/romote_user_place_order", "{\"user_id\":1,\"product_id\":1,\"product_count\":1}");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @RequestMapping("/publish")
    public void sendMessage() {
        redisChannelPublisher.send_cctv_message(new RedisQueueMessage("[cctv新闻联播] 发布的消息=" + System.currentTimeMillis() + getRandomChar()));
        redisChannelPublisher.send_bbc_message(new RedisQueueMessage("[bbc online] 发布的消息=" + System.currentTimeMillis() + getRandomChar()));
    }


    private static char getRandomChar() {
        String str = "";
        int hightPos; //
        int lowPos;

        Random random = new Random();

        hightPos = (176 + Math.abs(random.nextInt(39)));
        lowPos = (161 + Math.abs(random.nextInt(93)));

        byte[] b = new byte[2];
        b[0] = (Integer.valueOf(hightPos)).byteValue();
        b[1] = (Integer.valueOf(lowPos)).byteValue();

        try {
            str = new String(b, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            System.out.println("错误");
        }

        return str.charAt(0);
    }
}
